package com.gl.ruanzhu.controller;


import com.gl.basis.common.pojo.CommonResult;
import com.gl.ruanzhu.model.ApplicationInfo;
import com.gl.ruanzhu.service.IInspectionAndTestingServer;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/InspectionAndTesting")
@Slf4j
public class InspectionAndTestingController {
    private static final Logger logger = LoggerFactory.getLogger(FileController.class);

    @Autowired
    IInspectionAndTestingServer inspectionAndTestingServer;
    /**
     * 根据id获取项目详情
     */
    @PostMapping("/getProjectetailsDByid")
    @ResponseBody
    public CommonResult<?> getProjectetailsDByid(@RequestBody Map map) {
       try {
           Map getProjectetailsDByidMap=inspectionAndTestingServer.getProjectetailsDByid(map);
           return CommonResult.success(getProjectetailsDByidMap);
       }catch (Exception e){
           e.printStackTrace();
           return CommonResult.failed();
       }
      //  return inspectionAndTestingServer.getProjectetailsDByid(map);
    }

    /**
     * 获取检测项目集合
     * @return
     */
    @PostMapping("/getInspectionlist")
    @ResponseBody
    public  CommonResult<?> getInspectionlist(@RequestBody  Map map) {
        if ("全部".equals(map.get("detectionField"))){
            map.remove("detectionField");
            map.put("detectionField","");
        }
        if ("全部".equals(map.get("industryCategory"))){
            map.remove("industryCategory");
            map.put("industryCategory","");

        }
        if ("全部".equals(map.get("instruArea"))){
            map.remove("instruArea");
            map.put("instruArea","");

        }

        try {
            List<Object> returnlist=new ArrayList();
            List<Map> getInspectionlist=inspectionAndTestingServer.getInspectionlist((String)map.get("detectionField"),(String)map.get("industryCategory"),(int)map.get("sortType"),(String)map.get("instruArea"),(int)map.get("currPage") , (int)map.get("pageSize") );
            int total=inspectionAndTestingServer.getCountotal((String)map.get("detectionField"),(String)map.get("industryCategory"), (String)map.get("instruArea"));
            returnlist.add(getInspectionlist);
            Map totalmap=new HashMap();
            totalmap.put("total",total);
            returnlist.add(totalmap);
            return CommonResult.success(returnlist);
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed();
        }

    }

    @PostMapping("/addApplication")
    @ResponseBody
    public CommonResult<?> addApplication(@RequestBody ApplicationInfo applicationInfo) {
        try {
             inspectionAndTestingServer.addApplication(applicationInfo);
            return CommonResult.success("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed();
        }

    }


    @PostMapping("/getRecommendProject")
    @ResponseBody
    public CommonResult<?> getRecommendProject() {
        try {
          List<Map> RecommendProjects=  inspectionAndTestingServer.getRecommendProject();
            return CommonResult.success(RecommendProjects);
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed();
        }

    }

}
